using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CrazyDogToDo.Service.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using CrazyToDo.Web.Models;
using CrazyDogToDo.Entity.Data;

namespace CrazyToDo.Web.Controllers
{
    public class UserController : Controller
    {
        string myDayName = "我的一天";
        private UserService _userService = new UserService();
        private InventoryService _inventoryService = new InventoryService();
        public IActionResult Index()
        {
            if (HttpContext.Session.GetString("user") != null)
            {
                return RedirectToAction("Index", "Home");
            }
               
            else
                return View("Login");
        }
        /// <summary>
        /// 获取用户头像路径
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetHead()
        {
            var head = _userService.FindUserByEmail(HttpContext.Session.GetString("user")).UserAvatar;
            return head;
        }
        [HttpPost]
        public IActionResult Login(string email,string password)
        {
            if (_userService.Login(email, password))
            {
                if (_inventoryService.SearchInventory(email, DateTime.Now))
                {
                   HttpContext.Session.SetString("user",email);
                    return RedirectToAction("Index");
                }
                else
                {
                    _inventoryService.CreateInventory(email, myDayName);
                   HttpContext.Session.SetString("user", email);
                    return RedirectToAction("Index");
                }
            }
            else
                return View("Login");
        }

        public IActionResult Connect()
        {
            return View("SingUp");
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="email"></param>
        /// <param name="nickName"></param>
        /// <param name="password"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SingUp(string email,string nickName,string password,string sex)
        {
            bool gender = true;
            if (sex == "1")
                gender = true;
            else
                gender = false;
            if (_userService.Register(email, nickName, password, gender, 0) && _inventoryService.CreateInventory(email, myDayName))
            {
                return View("Login");
            }
            else
                return View("SingUp");
        }
        [HttpGet]
        /// <summary>
        /// 个人主页
        /// </summary>
        /// <returns></returns>
        public IActionResult PersonalHomepage()
        { 
            var user = _userService.FindUserByEmail(HttpContext.Session.GetString("user"));
            UserViewModel userViewModel = new UserViewModel()
            {
                Email = user.Email,
                NickName = user.NickName,              
                UserAvatar = user.UserAvatar,
                Gender = user.Gender,
                Score = user.Score
            };
            return View("PersonalHomepage", userViewModel);
        }
        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="nickname"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ModifyNickName(string nickname)
        {
            string email = HttpContext.Session.GetString("user");
            _userService.ModifyNickName(email, nickname);
            return RedirectToAction("PersonalHomepage");
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ModifyPassword(string password, string newPassword)
        {
            string email = HttpContext.Session.GetString("user");
            var statue = _userService.ModifyPassword(email, password, newPassword);
            var tipViewModel = new TipViewModel() {
                IsSuccess = statue
            };
            return View("ModifyPasswordTip", tipViewModel);                  
        }
        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="imgBase"></param>
        /// <returns></returns>
        [HttpPost]
        public string ChangeUserAvatar(string imgBase)
        {          
            var isSuccess =  _userService.ChangeUserAvatar(HttpContext.Session.GetString("user"), imgBase);
            if (isSuccess == true)
                return "修改头像成功！";
            else
                return "修改头像失败！";
        }

        public IActionResult Exit()
        {
            HttpContext.Session.Remove("user");
            return RedirectToAction("Index");
        }
    }
}